import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Start {
	private WebDriver driver;

	private String baseUrl;
	private String basUrl_direct;
	private boolean acceptNextAlert = true;
	private StringBuffer verificationErrors = new StringBuffer();
	private String username;
	private String Password;

	@Before
	public void setUp() throws Exception {

		driver = new FirefoxDriver(); // new HtmlUnitDriver();//

		baseUrl = "https://rutabagad1v.aixlan.local:8443/cas/login?TARGET=https%3A%2F%2Frutabagad1v.aixlan.local%3A8443%2FWebAdmin%2Fauth%2Fapps.do";
		basUrl_direct="https://rutabagad1v.aixlan.local:8413/octroi-web";
		username = "asmari";
		Password = "k53(Z!(Q8fc}_D@p";

		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	}

	@Test
	public void testStart() throws Exception {
		driver.get(basUrl_direct);
		driver.findElement(By.id("username")).clear();
		driver.findElement(By.id("username")).sendKeys(username);
		driver.findElement(By.id("password")).clear();
		driver.findElement(By.id("password")).sendKeys(Password);
		driver.findElement(By.id("submit")).click();
		//driver.findElement(By.linkText("Octroi"))
			//	.click();
		driver.findElement(
				By.xpath("//img[@src='/octroi-web/images/menu_ppa.png']"))
				.click();
		driver.findElement(
				By.xpath("//img[@src='/octroi-web/images/menu_nouvelledemande.png']"))
				.click();
		driver.findElement(By.id("annuler")).click();
		driver.findElement(
				By.xpath("//img[@src='/octroi-web/images/menu_ppa.png']"))
				.click();
		// driver.findElement(By.id("sousMenuFormId_menu")).click();
		driver.findElement(By.id("deconnexion")).click();
	}

	@After
	public void tearDown() throws Exception {
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}

	private boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}

	private boolean isAlertPresent() {
		try {
			driver.switchTo().alert();
			return true;
		} catch (NoAlertPresentException e) {
			return false;
		}
	}

	private String closeAlertAndGetItsText() {
		try {
			Alert alert = driver.switchTo().alert();
			String alertText = alert.getText();
			if (acceptNextAlert) {
				alert.accept();
			} else {
				alert.dismiss();
			}
			return alertText;
		} finally {
			acceptNextAlert = true;
		}
	}
}
